from datetime import datetime
from typing import TYPE_CHECKING, List

from sqlalchemy import Column, DateTime, ForeignKey, Integer, String
from sqlalchemy.orm import relationship

from app.db.base_class import Base

if TYPE_CHECKING:
    from .messages import Message  # noqa: F401
    from .sessions import Session  # noqa: F401


class RelationshipType:
    FRIEND = "friend"
    COLLEAGUE = "colleague"
    SUPERIOR = "superior"
    SUBORDINATE = "subordinate"
    CLIENT = "client"
    FAMILY = "family"
    OTHER = "other"


class Contact(Base):
    """联系人信息表"""
    id = Column(Integer, primary_key=True, index=True)
    wechat_id = Column(String(100), unique=True, index=True, nullable=False, comment="微信ID")
    nickname = Column(String(100), nullable=False, comment="昵称")
    remark = Column(String(100), nullable=True, comment="备注名")
    relationship_type = Column(
        String(50), 
        default=RelationshipType.OTHER, 
        comment="关系类型"
    )
    first_interaction = Column(DateTime, default=datetime.utcnow, comment="首次交互时间")
    last_interaction = Column(
        DateTime, 
        default=datetime.utcnow,
        onupdate=datetime.utcnow,
        comment="最后交互时间"
    )
    description = Column(String(500), nullable=True, comment="联系人描述")
    
    # 关系
    messages = relationship("Message", back_populates="contact", cascade="all, delete-orphan")
    sessions = relationship("Session", back_populates="contacts", secondary="session_contact")
    
    def __repr__(self):
        return f"<Contact {self.nickname}>"